﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using Moq;
using Xpedite.Resources;
using Xpedite.Resources.Definitions;
using Resource = Xpedite.Mvc.Html.Resource;

/* Copyright (c) 2010 CBaxter
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
 * IN THE SOFTWARE. 
 */

namespace Xpedite.Mvc.Test.Html
{
  internal static class HtmlHelperUtil
  {
    internal static HtmlHelper ExpectCompositeResourceCreated(ResourceType resourceType, String referenceName, IEnumerable<Resource> resources)
    {
      var matcher = new Predicate<IEnumerable<INamedValue<IResourceUrl>>>(namedValues =>
      {
        foreach (var namedValue in namedValues)
        {
          if (resources.Where(resource => resource.Name == namedValue.Name && resource.ResourceUrl == namedValue.Value.VirtualPath).Count() != 1)
            return false;
        }

        return true;
      });

      var definition = new Mock<IResourceDefinition>(MockBehavior.Strict);
      definition.Setup(mock => mock.ReferenceResources(It.Is<IEnumerable<INamedValue<IResourceUrl>>>(value => matcher.Invoke(value))));
      definition.SetupGet(mock => mock.ReferenceName).Returns(referenceName);
      definition.SetupGet(mock => mock.ResourceType).Returns(resourceType);
      definition.Setup(mock => mock.EmitResource(It.IsAny<HtmlTextWriter>()));

      var definitionFactory = new Mock<IResourceDefinitionFactory>(MockBehavior.Strict);
      definitionFactory.Setup(mock => mock.Create(resourceType, referenceName)).Returns(definition.Object);

      var httpApplicationState = new Mock<HttpApplicationStateBase>(MockBehavior.Strict);
      httpApplicationState.SetupGet(mock => mock[typeof(ResourceDefinitionFactory).AssemblyQualifiedName]).Returns(definitionFactory.Object);

      var httpRequest = new Mock<HttpRequestBase>(MockBehavior.Strict);
      httpRequest.SetupGet(mock => mock.IsLocal).Returns(false);
      httpRequest.SetupGet(mock => mock.ApplicationPath).Returns("/");
      httpRequest.SetupGet(mock => mock.Url).Returns(new Uri("http://localhost/Home/Index"));

      foreach (var resource in resources)
        httpRequest.Setup(mock => mock.MapPath(resource.ResourceUrl)).Returns(@"C:\" + resource.ResourceUrl.Replace('/', '\\'));

      var httpResponse = new Mock<HttpResponseBase>(MockBehavior.Strict);

      return MockHtmlHelperFactory.Create(httpApplicationState.Object, httpRequest.Object, httpResponse.Object);
    }
  }
}
